<?php
/**
 * Class that operate on table 'searches'. Database Sqlite.
 *
 * @author: Dragan Milchevski
 * @date: 2012-06-23 13:23
 */
class SearchesSqliteDAO implements SearchesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return SearchesSqlite 
	 */
	public function load($id){
		$sql = 'SELECT * FROM searches WHERE searchID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM searches';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM searches ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param searche primary key
 	 */
	public function delete($searchID){
		$sql = 'DELETE FROM searches WHERE searchID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($searchID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param SearchesSqlite searche
 	 */
	public function insert($searche){
		$sql = 'INSERT INTO searches (userID, searchname, searchfield, searchvalue) VALUES (?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($searche->userID);
		$sqlQuery->set($searche->searchname);
		$sqlQuery->set($searche->searchfield);
		$sqlQuery->set($searche->searchvalue);

		$id = $this->executeInsert($sqlQuery);	
		$searche->searchID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param SearchesSqlite searche
 	 */
	public function update($searche){
		$sql = 'UPDATE searches SET userID = ?, searchname = ?, searchfield = ?, searchvalue = ? WHERE searchID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($searche->userID);
		$sqlQuery->set($searche->searchname);
		$sqlQuery->set($searche->searchfield);
		$sqlQuery->set($searche->searchvalue);

		$sqlQuery->setNumber($searche->searchID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM searches';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByUserID($value){
		$sql = 'SELECT * FROM searches WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySearchname($value){
		$sql = 'SELECT * FROM searches WHERE searchname = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySearchfield($value){
		$sql = 'SELECT * FROM searches WHERE searchfield = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryBySearchvalue($value){
		$sql = 'SELECT * FROM searches WHERE searchvalue = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByUserID($value){
		$sql = 'DELETE FROM searches WHERE integer = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySearchname($value){
		$sql = 'DELETE FROM searches WHERE text = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySearchfield($value){
		$sql = 'DELETE FROM searches WHERE text = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteBySearchvalue($value){
		$sql = 'DELETE FROM searches WHERE text = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return SearchesSqlite 
	 */
	protected function readRow($row){
		$searche = new Searche();
		
		$searche->searchID = $row['searchID'];
		$searche->userID = $row['userID'];
		$searche->searchname = $row['searchname'];
		$searche->searchfield = $row['searchfield'];
		$searche->searchvalue = $row['searchvalue'];

		return $searche;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return SearchesSqlite 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>